/*
 * =====================================================================================
 *
 *       Filename:  redis_conn.h
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  03/15/2018 04:22:20 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *   Organization:  
 *
 * =====================================================================================
 */

#ifndef REDIS_CONN_H
#define REDIS_CONN_H

#include "hiredis.h"

#include <sys/time.h>
#include <string>
#include <vector>


using std::string;
using std::vector;

class RedisCluster;

class RedisConn
{
public:
    RedisConn(const string &ip, unsigned int port,
            const struct timeval& tmout);
    ~RedisConn();

    bool connect();
    bool connected();
    // 适用于不需要解析响应的一些命令
    bool exec(const string& cmd);

	bool getKey(const string& key,string& value);
	bool setKeyValue(const string& key, const string &value, const string &ttl = "1800");

public:
	static int stringSplit(const string& str, const string& sep, vector<string> &vec );
private:
    RedisConn(const RedisConn& other);
    RedisConn& operator=(const RedisConn& other);
    
	string to_lower(const string& src);
	// 重新加载变化后的集群配置
	RedisConn* getMovedConn(const string& errmsg);

    friend class RedisCluster;
    // 初始化cluster的 m_nodes 和 m_conns
    bool loadClusterSlots(const struct timeval& tmout);
private:
    redisContext    *m_ctx;
    string          m_ip;
    unsigned int    m_port;
    struct timeval  m_tmout;
};




#endif







